package project.euler.honk.problem.to50;


public final class Problem45 {
	
	private static long getTri(int n) {
		long  b_n = n;
		long b_n2 = n + 1;
		return b_n * b_n2 / 2;
	}
	
	private static long getPen(int n) {
		long b_n = n;
		long b_n2 = 3 * n - 1;
		return b_n * b_n2 / 2;
	}
	
	private static long getHex(int n) {
		long b_n = n;
		long b_n2 = 2 * n - 1;
		return b_n * b_n2;
	}
	
	public static long solve() {
		
		int n_tri = 286;
		int n_pen = 166;
		int n_hex = 144;
		
		long tri = getTri(n_tri);
		long pen = getPen(n_pen);
		long hex = getHex(n_hex);
		
		while(true) {
			if(tri == pen && tri == hex) return tri;
				
			if(pen < tri) {
				pen = getPen(++n_pen);
			} else if(hex < tri) {
				hex = getHex(++n_hex);
			} else {
				tri = getTri(++n_tri);
			}
		}
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		long result = solve();
		long end = System.currentTimeMillis();
		System.out.println("Result: " + result + " Time: " + (end - start));
	}

}
